package com.example.mycnblog.controller;

import com.example.mycnblog.common.AjaxResult;
import com.example.mycnblog.common.AppVariable;
import com.example.mycnblog.common.PasswordUtils;
import com.example.mycnblog.common.UserSessionUtils;
import com.example.mycnblog.entity.Articleinfo;
import com.example.mycnblog.entity.Userinfo;
import com.example.mycnblog.entity.vo.UserinfoVO;
import com.example.mycnblog.service.ArticleService;
import com.example.mycnblog.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/reg")
    public AjaxResult reg(Userinfo userinfo) {
        //非空校验和参数有效性校验     前端不可信
        if (userinfo == null || !StringUtils.hasLength(userinfo.getUsername()) || !StringUtils.hasLength(userinfo.getPassword())) {
            return AjaxResult.fail(-1, "非法参数");
        }
        //进行密码加密
        userinfo.setPassword(PasswordUtils.encrypt(userinfo.getPassword()));
        return AjaxResult.success(userService.reg(userinfo));

    }

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request,String username, String password) {
        //非空校验
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.fail(-1, "非法请求");
        }
        //查询数据库
        Userinfo userinfo = userService.getUserByName(username);
        if (userinfo != null && userinfo.getId() > 0) {
            //有效用户
            // 两个密码是否相同
            if (PasswordUtils.check(password,userinfo.getPassword())) {

                //登录成功
                //将用户存储到session中，request对象中
                HttpSession session=request.getSession();
                session.setAttribute(AppVariable.USER_SESSION_KEY,userinfo);
                userinfo.setPassword("");//返回前端前，隐藏敏感（密码）信息
                return AjaxResult.success(userinfo);
            }
        }
        return AjaxResult.success(0, null);
    }

    @RequestMapping("/showinfo")
    public AjaxResult showInfo(HttpServletRequest request){
        UserinfoVO userinfoVO=new UserinfoVO();
        //得到当前登录用户(从session中获取)
        Userinfo userinfo= UserSessionUtils.getUser(request);
        if(userinfo==null){
            return AjaxResult.fail(-1,"非法请求");
        }
        //Spring提供的深克隆方法
        BeanUtils.copyProperties(userinfo,userinfoVO);
        //得到用户发表文章的总数
        userinfoVO.setArtCount(articleService.getArtCountByUid(userinfo.getId()));
        return AjaxResult.success(userinfoVO);
    }

    //注销
    @RequestMapping("/logout")
    public AjaxResult logout(HttpSession session){
        session.removeAttribute(AppVariable.USER_SESSION_KEY);
        return AjaxResult.success(1);
    }

    @RequestMapping("/getuserbyid")
    public AjaxResult getUserById(Integer id){
        if(id==null||id<0){
            return AjaxResult.fail(-1,"非法参数");
        }
        Userinfo userinfo=userService.getUserById(id);
        if(userinfo==null||userinfo.getId()<=0){
            return AjaxResult.fail(-1,"非法参数");
        }
        //去除userinfo中的敏感数据，eg：密码
        userinfo.setPassword("");
        //查询当前用户发表的文章数
        UserinfoVO userinfoVO=new UserinfoVO();
        BeanUtils.copyProperties(userinfo,userinfoVO);
        userinfoVO.setArtCount(articleService.getArtCountByUid(id));
        return AjaxResult.success(userinfoVO);
    }


}
